/**
 * @author trex279
 */

function loadingStartAction(event){
	$(".content").css("display","none");
	$(".loader").css("display","inline");
}

function loadingCompleteAction(event){
	$(".loader").css("display","none");
	$(".content").css("display","block");
}

function loadingErrorAction(event){
	$(".content").html("Please login in the \"Login\" tab");
	$(".content").css("display","block");
	$(".loader").css("display","none");
}

thisApplication.initialize = function(){
	thisApplication.nativeWin = window.nativeWindow;
	thisApplication.nativeWin.addEventListener(air.NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING, thisApplication.minimizeToTray);
	thisApplication.nativeWin.addEventListener(air.Event.EXITING, thisApplication.exit);
	thisApplication.nativeWin.addEventListener('loadingStart',loadingStartAction);
	thisApplication.nativeWin.addEventListener('loadingComplete',loadingCompleteAction);
	thisApplication.nativeWin.addEventListener('loadingError',loadingErrorAction);
	thisApplication.createMenus();
}; //the global object for a lot of functions

thisApplication.minimizeToTray = function(event){
	event.preventDefault();
	if (event.afterDisplayState == air.NativeWindowDisplayState.MAXIMIZED){
		thisApplication.nativeWin.maximize();
	}
	if (event.afterDisplayState == air.NativeWindowDisplayState.NORMAL){
		thisApplication.nativeWin.restore();
	}
	else
	if (event.afterDisplayState == air.NativeWindowDisplayState.MINIMIZED) {
		thisApplication.nativeWin.minimize();
		thisApplication.nativeWin.visible = false;
		
		//to show the icon in the system tray
		var iconLoadComplete = function(event) 
	    { 
	        air.NativeApplication.nativeApplication.icon.bitmaps = [event.target.content.bitmapData]; 
	    }
		
		air.NativeApplication.nativeApplication.autoExit = false;
		 
	    var iconLoad = new air.Loader(); 
	    var iconMenu = new air.NativeMenu(); 
		
	    var exitCommand = iconMenu.addItem(new air.NativeMenuItem("Exit")); 
	    exitCommand.addEventListener(air.Event.SELECT,function(event){
	            thisApplication.exit();
	    }); 
	 
	    if (air.NativeApplication.supportsSystemTrayIcon) { 
	        air.NativeApplication.nativeApplication.autoExit = false; 
	        iconLoad.contentLoaderInfo.addEventListener(air.Event.COMPLETE,iconLoadComplete); 
	        iconLoad.load(new air.URLRequest("img/alien16.png")); 
	        air.NativeApplication.nativeApplication.icon.tooltip = "Reddit Notifier"; 
	        air.NativeApplication.nativeApplication.icon.menu = iconMenu;
			 
			air.NativeApplication.nativeApplication.icon.addEventListener("click", restoreWindow);
			function restoreWindow(event){
				thisApplication.nativeWin.visible = true;
				thisApplication.nativeWin.restore();
				thisApplication.nativeWin.activate();
				air.NativeApplication.nativeApplication.icon.bitmaps = [];
				iconLoad.unload();
			}
	    } 
	 
	    if (air.NativeApplication.supportsDockIcon) { 
	        iconLoad.contentLoaderInfo.addEventListener(air.Event.COMPLETE,iconLoadComplete); 
	        iconLoad.load(new air.URLRequest("img/alien16.png")); 
	        air.NativeApplication.nativeApplication.icon.menu = iconMenu;
	    } 
	}
}

thisApplication.createMenus = function() { 
	var menu = air.ui.Menu.createFromXML("menu.xml"); 
	air.ui.Menu.setAsMenu(menu); 
}

thisApplication.exit = function(){
	var event = new air.Event(air.Event.EXITING, false, true); 
	air.NativeApplication.nativeApplication.dispatchEvent(event); 
	if (!event.isDefaultPrevented()) {
		air.NativeApplication.nativeApplication.icon.bitmaps = [];
 		air.NativeApplication.nativeApplication.exit(); 
	}
}

thisApplication.showNotification = function(template,injectContentFunction){
	var NOTIFY_SOURCE = template;
	var WINDOW_WIDTH = 139;//200;
	var WINDOW_HEIGHT = 75;//50;
	
	// Variables related to the new native window
	var bounds = null;
	var notification = null;
	var options = new air.NativeWindowInitOptions();
	var screen = air.Screen.mainScreen.visibleBounds;

	// Setup the new window
	// Use lightweight to remove chrome
	// Set window to transparent
	// Explicitly specify to not use system chrome
	options.type = air.NativeWindowType.LIGHTWEIGHT;
	options.transparent = true;
	options.systemChrome = air.NativeWindowSystemChrome.NONE;

	// Figure out where to put the window on the screen
	bounds = new air.Rectangle(
		screen.width - WINDOW_WIDTH - 10,
		screen.height - WINDOW_HEIGHT - 30,
		WINDOW_WIDTH,
		WINDOW_HEIGHT
	);

	// Create a new native window and get a handle to the HTML viewport
	notification = air.HTMLLoader.createRootWindow(true, options, false, bounds);
	
	// Tell browser not to paint a default background
	// Position the new window in front of everything else
	// Any links open in browser registered to the system
	notification.paintsDefaultBackground = false;
	notification.stage.nativeWindow.alwaysInFront = true;
	notification.navigateInSystemBrowser = true;

/*
	notification.addEventListener(air.Event.COMPLETE, function(){
		//using direct js dom here 'coz jquery selector doesn't seem to work here. Some shady stuff done after much trial and error.
		if(notification.window.document) //so that the next statement is not triggered when the notification is closing
				notification.window.document.getElementById("noReplies").innerHTML = noNewReplies;
	});
*/
	
	notification.addEventListener(air.Event.COMPLETE, injectContent);
	
	 function injectContent(){
		if(notification.window.document) //so that the next statement is not triggered when the notification is closing
				injectContentFunction(notification.window.document);
	}
	
	// Load the page that represent the notification display
	// In this case a local HTML page included with the AIR application

	notification.load(new air.URLRequest(NOTIFY_SOURCE));
	
	
	//To close the notification window.
	var DELAY_CLOSE = 5000;//4000;
	var REPEAT_CLOSE = 1;
	
	var closeTimer = null;
		
	// Create a timer instance and listen for completion
	closeTimer = new air.Timer( DELAY_CLOSE, REPEAT_CLOSE );
	closeTimer.addEventListener( air.TimerEvent.TIMER_COMPLETE, doCloseTimerComplete );
	
	closeTimer.start();
	
	function doCloseTimerComplete(evt)
	{
		notification.removeEventListener(air.Event.COMPLETE, injectContent);
		notification.stage.nativeWindow.close();
	}
	//end notification close

}